﻿using EkStudio.com.ui.unitbill;
using EkStudio.com.bean.xml.BillMapping;
using System.Data;
using System.Windows.Forms;
using EkStudio.com.bean.xml.ProjectMenu;
using DevExpress.XtraEditors.Controls;
using EkStudio.com.bean.xml.BillModule;
using DevExpress.XtraEditors;
using EkStudio.com.ui.unitbill.property.Event;
using System.Threading;
using EkStudio.com.common.unit;
using EkStudio.com.bean.xml.reflection.JAXB;
using System;
using System.Collections.Generic;
using EkStudio.com.bean.xml.RootInterface.Field;
using EkStudio.com.bean.xml.Unit;

namespace EkStudio.com.ui.form.MapDataSource
{
    /// <summary>
    /// 仓库选择库存模块
    /// </summary>
    public partial class MapStockClassForm : DevExpress.XtraEditors.XtraForm
    {
        private BillDataMapItem Map = null;
        private ProjectRootPanel RootPanel = null;
        private BillMapUnitPanel UnitPanel = null;

        private BillModuleItem bill = null;

        private DataTable group = null,stock = null,monthIn = null,monthOut = null,dayIn = null,dayOut = null;
        private DataTable LastData = null;

        private int mainThreadId = -1;

        private List<FieldSelectItem> fieldList = new List<FieldSelectItem>();

        public MapStockClassForm(ProjectRootPanel root,BillMapUnitPanel unit,BillDataMapItem bean)
        {
            InitializeComponent();
            Map = bean;
            RootPanel = root;
            UnitPanel = unit;
            InitForm();
        }

        private void InitForm()
        {
            mainThreadId = Thread.CurrentThread.ManagedThreadId;
            //初始化目录列表
            if (RootPanel.MenuItem!=null&&RootPanel.MenuItem.Business!=null
                && RootPanel.MenuItem.Business.Modules != null && RootPanel.MenuItem.Business.Modules.Count > 0){
                ImageComboBoxItem select = new ImageComboBoxItem("请选择单据...",null,6);
                ModComboBoxEdit.Properties.Items.Add(select);
                foreach (ProjectMenuModGroup mod in RootPanel.MenuItem.Business.Modules)
                {
                    if (mod.Module != null && mod.Module.Count > 0)
                    {
                        foreach(ProjectMenuCommonElement it in mod.Module)
                        {
                            ImageComboBoxItem item = new ImageComboBoxItem(it.Caption, it, 6);
                            ModComboBoxEdit.Properties.Items.Add(item);
                        }
                    }
                }
            }

            //字段分组类型
            RepositoryGroupTypeEdit.Items.Add(BeanUtil.MAP_STDATETIME);
            RepositoryGroupTypeEdit.Items.Add(BeanUtil.MAP_STGROUP);

            //初始化分组
            group = new DataTable("GroupTable");
            group.Columns.Add("FieldObject", typeof(FieldSelectItem));
            group.Columns.Add("FieldCaption", typeof(string));
            group.Columns.Add("FieldKey", typeof(string));
            group.Columns.Add("FieldDataType", typeof(string));
            group.Columns.Add("FieldDataSource", typeof(string));
            group.Columns.Add("languageTexts", typeof(string));
            group.Columns.Add("GroupType",typeof(string));
            GridGroupControl.DataSource = group;
            //初始化结存分组
            stock = new DataTable("StockTable");
            stock.Columns.Add("caption", typeof(string));
            stock.Columns.Add("key", typeof(string));
            stock.Columns.Add("typeNode", typeof(string));
            stock.Columns.Add("FieldCaption", typeof(string));
            stock.Columns.Add("FieldKey", typeof(string));
            stock.Columns.Add("FieldDataType", typeof(string));
            stock.Columns.Add("FieldDataSource", typeof(string));
            stock.Columns.Add("languageTexts", typeof(string));
            GridRealControl.DataSource = stock;

            //初始化月入分组
            monthIn = new DataTable("StockIntoTable");
            monthIn.Columns.Add("caption", typeof(string));
            monthIn.Columns.Add("key", typeof(string));
            monthIn.Columns.Add("typeNode", typeof(string));
            monthIn.Columns.Add("FieldCaption", typeof(string));
            monthIn.Columns.Add("FieldKey", typeof(string));
            monthIn.Columns.Add("FieldDataType", typeof(string));
            monthIn.Columns.Add("FieldDataSource", typeof(string));
            monthIn.Columns.Add("languageTexts", typeof(string));
            GridMonInControl.DataSource = monthIn;

            //初始化月出分组
            monthOut = new DataTable("StockOutTable");
            monthOut.Columns.Add("caption", typeof(string));
            monthOut.Columns.Add("key", typeof(string));
            monthOut.Columns.Add("typeNode", typeof(string));
            monthOut.Columns.Add("FieldCaption", typeof(string));
            monthOut.Columns.Add("FieldKey", typeof(string));
            monthOut.Columns.Add("FieldDataType", typeof(string));
            monthOut.Columns.Add("FieldDataSource", typeof(string));
            monthOut.Columns.Add("languageTexts", typeof(string));
            GridMonthOutControl.DataSource = monthOut;

            //初始化期初分组
            LastData = new DataTable("StockLastTable");
            LastData.Columns.Add("caption", typeof(string));
            LastData.Columns.Add("key", typeof(string));
            LastData.Columns.Add("typeNode", typeof(string));
            LastData.Columns.Add("FieldCaption", typeof(string));
            LastData.Columns.Add("FieldKey", typeof(string));
            LastData.Columns.Add("FieldDataType", typeof(string));
            LastData.Columns.Add("FieldDataSource", typeof(string));
            LastData.Columns.Add("languageTexts", typeof(string));
            GridLastControl.DataSource = LastData;
            //初始化当日入分组
            dayIn = new DataTable("StockDayInTable");
            dayIn.Columns.Add("caption", typeof(string));
            dayIn.Columns.Add("key", typeof(string));
            dayIn.Columns.Add("typeNode", typeof(string));
            dayIn.Columns.Add("FieldCaption", typeof(string));
            dayIn.Columns.Add("FieldKey", typeof(string));
            dayIn.Columns.Add("FieldDataType", typeof(string));
            dayIn.Columns.Add("FieldDataSource", typeof(string));
            dayIn.Columns.Add("languageTexts", typeof(string));
            GridDayInControl.DataSource = dayIn;
            //初始化当日出分组
            dayOut = new DataTable("StockDayOutTable");
            dayOut.Columns.Add("caption", typeof(string));
            dayOut.Columns.Add("key", typeof(string));
            dayOut.Columns.Add("typeNode", typeof(string));
            dayOut.Columns.Add("FieldCaption", typeof(string));
            dayOut.Columns.Add("FieldKey", typeof(string));
            dayOut.Columns.Add("FieldDataType", typeof(string));
            dayOut.Columns.Add("FieldDataSource", typeof(string));
            dayOut.Columns.Add("languageTexts", typeof(string));
            GridDayOutControl.DataSource = dayOut;

        }



        /// <summary>
        /// 显示前加载初始化
        /// </summary>
        /// <returns></returns>
        new public DialogResult ShowDialog()
        {

            //加载配置数据
            


            //加载分组配置
            group.Rows.Clear();
            if (Map.MapProperties.Advanced == null) Map.MapProperties.Advanced = new BillDataMapAdvanced();
            if (Map.MapProperties.Advanced.Field == null) Map.MapProperties.Advanced.Field = new List<BillDataMapAdvancedElement>();
            HasDayInAndOutType.Checked = Map.MapProperties.Advanced.LogEnable;
            if (Map.MapProperties.Advanced.Field.Count > 0)
            {
                foreach(BillDataMapAdvancedElement it in Map.MapProperties.Advanced.Field)
                {
                    if(it.Command == BeanUtil.MAP_STDATETIME || it.Command == BeanUtil.MAP_STGROUP)
                    {
                        DataRow row = group.NewRow();
                        row["FieldObject"] = null;
                        row["FieldCaption"] = it.Caption;
                        row["FieldKey"] = it.Key;
                        row["FieldDataType"] = it.DataType;
                        row["FieldDataSource"] = it.DataSource;
                        row["languageTexts"] = it.LanguageTexts;
                        row["GroupType"] = it.Command;
                        group.Rows.Add(row);
                    }
                }
            }

            //加载结存配置
            stock.Rows.Clear();
            if (Map.MapProperties.Advanced.Field.Count > 0)
            {
                List<BillDataMapAdvancedElement> StockList = new List<BillDataMapAdvancedElement>();
                foreach(BillDataMapAdvancedElement it in Map.MapProperties.Advanced.Field)
                {
                    if(it.StockType == BeanUtil.STOCK_GROUP_ST_STOCK)
                    {
                        StockList.Add(it);
                    }
                }

                if(Map.MapProperties.Target!=null&&Map.MapProperties.Target.Element!=null
                    && Map.MapProperties.Target.Element.Count > 0)
                {
                    foreach(BillDataMapTargetElement it in Map.MapProperties.Target.Element)
                    {
                        DataRow row = stock.NewRow();
                        row["caption"] = it.Caption;
                        row["key"] = it.Key;
                        row["typeNode"] = it.NodeType;
                        row["FieldKey"] = "";
                        row["FieldCaption"] = "";
                        row["FieldDataType"] = "";
                        row["FieldDataSource"] = "";
                        row["languageTexts"] = "";

                        //查找已存在的字段列表
                        if (StockList.Count > 0)
                        {
                            foreach(BillDataMapAdvancedElement ele in StockList)
                            {
                                if(it.Key == ele.StockLinkKey)
                                {
                                    row["FieldKey"] = ele.Key;
                                    row["FieldCaption"] = ele.Caption;
                                    row["FieldDataType"] = ele.DataType;
                                    row["FieldDataSource"] = ele.DataSource;
                                    row["languageTexts"] = ele.LanguageTexts;
                                    StockList.Remove(ele);
                                    break;
                                }
                            }
                        }
                        stock.Rows.Add(row);
                    }
                }
                if (StockList.Count > 0)//无效的列表
                {
                    foreach(BillDataMapAdvancedElement ele in StockList)
                    {
                        DataRow row = stock.NewRow();
                        row["caption"] = "无效的数据";
                        row["key"] = ele.StockLinkKey;
                        row["typeNode"] = "";
                        row["FieldKey"] = ele.Key;
                        row["FieldCaption"] = ele.Caption;
                        row["FieldDataType"] = ele.DataType;
                        row["FieldDataSource"] = ele.DataSource;
                        row["languageTexts"] = ele.LanguageTexts;
                        stock.Rows.Add(row);
                    }
                }

            }
            else
            {
                if (Map.MapProperties.Target != null && Map.MapProperties.Target.Element != null
                    && Map.MapProperties.Target.Element.Count > 0)
                {
                    foreach (BillDataMapTargetElement it in Map.MapProperties.Target.Element)
                    {
                        DataRow row = stock.NewRow();
                        row["caption"] = it.Caption;
                        row["key"] = it.Key;
                        row["typeNode"] = it.NodeType;
                        row["FieldKey"] = "";
                        row["FieldCaption"] = "";
                        row["FieldDataType"] = "";
                        row["FieldDataSource"] = "";
                        row["languageTexts"] = "";
                        stock.Rows.Add(row);
                    }
                }
            }

            //加载月入配置
            monthIn.Rows.Clear();
            if (Map.MapProperties.Advanced.Field.Count > 0)
            {
                List<BillDataMapAdvancedElement> StockList = new List<BillDataMapAdvancedElement>();
                foreach (BillDataMapAdvancedElement it in Map.MapProperties.Advanced.Field)
                {
                    if (it.StockType == BeanUtil.STOCK_GROUP_ST_INTO_STOCK)
                    {
                        StockList.Add(it);
                    }
                }

                if (Map.MapProperties.Target != null && Map.MapProperties.Target.Element != null
                    && Map.MapProperties.Target.Element.Count > 0)
                {
                    foreach (BillDataMapTargetElement it in Map.MapProperties.Target.Element)
                    {
                        if(it.NodeType != BeanUtil.STOCK_MAP_FIELD_PRICE)//单价不出现在类型中
                        {
                            DataRow row = monthIn.NewRow();
                            row["caption"] = it.Caption;
                            row["key"] = it.Key;
                            row["typeNode"] = it.NodeType;
                            row["FieldKey"] = "";
                            row["FieldCaption"] = "";
                            row["FieldDataType"] = "";
                            row["FieldDataSource"] = "";
                            row["languageTexts"] = "";

                            //查找已存在的字段列表
                            if (StockList.Count > 0)
                            {
                                foreach (BillDataMapAdvancedElement ele in StockList)
                                {
                                    if (it.Key == ele.StockLinkKey)
                                    {
                                        row["FieldKey"] = ele.Key;
                                        row["FieldCaption"] = ele.Caption;
                                        row["FieldDataType"] = ele.DataType;
                                        row["FieldDataSource"] = ele.DataSource;
                                        row["languageTexts"] = ele.LanguageTexts;
                                        StockList.Remove(ele);
                                        break;
                                    }
                                }
                            }
                            monthIn.Rows.Add(row);
                        }
                    }
                }
                if (StockList.Count > 0)//无效的列表
                {
                    foreach (BillDataMapAdvancedElement ele in StockList)
                    {
                        DataRow row = monthIn.NewRow();
                        row["caption"] = "无效的数据";
                        row["key"] = ele.StockLinkKey;
                        row["typeNode"] = "";
                        row["FieldKey"] = ele.Key;
                        row["FieldCaption"] = ele.Caption;
                        row["FieldDataType"] = ele.DataType;
                        row["FieldDataSource"] = ele.DataSource;
                        row["languageTexts"] = ele.LanguageTexts;
                        monthIn.Rows.Add(row);
                    }
                }

            }
            else
            {
                if (Map.MapProperties.Target != null && Map.MapProperties.Target.Element != null
                    && Map.MapProperties.Target.Element.Count > 0)
                {
                    foreach (BillDataMapTargetElement it in Map.MapProperties.Target.Element)
                    {
                        DataRow row = monthIn.NewRow();
                        row["caption"] = it.Caption;
                        row["key"] = it.Key;
                        row["typeNode"] = it.NodeType;
                        row["FieldKey"] = "";
                        row["FieldCaption"] = "";
                        row["FieldDataType"] = "";
                        row["FieldDataSource"] = "";
                        row["languageTexts"] = "";
                        monthIn.Rows.Add(row);
                    }
                }
            }

            //加载月出配置
            monthOut.Rows.Clear();
            if (Map.MapProperties.Advanced.Field.Count > 0)
            {
                List<BillDataMapAdvancedElement> StockList = new List<BillDataMapAdvancedElement>();
                foreach (BillDataMapAdvancedElement it in Map.MapProperties.Advanced.Field)
                {
                    if (it.StockType == BeanUtil.STOCK_GROUP_ST_OUT_STOCK)
                    {
                        StockList.Add(it);
                    }
                }

                if (Map.MapProperties.Target != null && Map.MapProperties.Target.Element != null
                    && Map.MapProperties.Target.Element.Count > 0)
                {
                    foreach (BillDataMapTargetElement it in Map.MapProperties.Target.Element)
                    {
                        if (it.NodeType != BeanUtil.STOCK_MAP_FIELD_PRICE)//单价不出现在类型中
                        {
                            DataRow row = monthOut.NewRow();
                            row["caption"] = it.Caption;
                            row["key"] = it.Key;
                            row["typeNode"] = it.NodeType;
                            row["FieldKey"] = "";
                            row["FieldCaption"] = "";
                            row["FieldDataType"] = "";
                            row["FieldDataSource"] = "";
                            row["languageTexts"] = "";

                            //查找已存在的字段列表
                            if (StockList.Count > 0)
                            {
                                foreach (BillDataMapAdvancedElement ele in StockList)
                                {
                                    if (it.Key == ele.StockLinkKey)
                                    {
                                        row["FieldKey"] = ele.Key;
                                        row["FieldCaption"] = ele.Caption;
                                        row["FieldDataType"] = ele.DataType;
                                        row["FieldDataSource"] = ele.DataSource;
                                        row["languageTexts"] = ele.LanguageTexts;
                                        StockList.Remove(ele);
                                        break;
                                    }
                                }
                            }
                            monthOut.Rows.Add(row);
                        }
                    }
                }
                if (StockList.Count > 0)//无效的列表
                {
                    foreach (BillDataMapAdvancedElement ele in StockList)
                    {
                        DataRow row = monthOut.NewRow();
                        row["caption"] = "无效的数据";
                        row["key"] = ele.StockLinkKey;
                        row["typeNode"] = "";
                        row["FieldKey"] = ele.Key;
                        row["FieldCaption"] = ele.Caption;
                        row["FieldDataType"] = ele.DataType;
                        row["FieldDataSource"] = ele.DataSource;
                        row["languageTexts"] = ele.LanguageTexts;
                        monthOut.Rows.Add(row);
                    }
                }

            }
            else
            {
                if (Map.MapProperties.Target != null && Map.MapProperties.Target.Element != null
                    && Map.MapProperties.Target.Element.Count > 0)
                {
                    foreach (BillDataMapTargetElement it in Map.MapProperties.Target.Element)
                    {
                        DataRow row = monthOut.NewRow();
                        row["caption"] = it.Caption;
                        row["key"] = it.Key;
                        row["typeNode"] = it.NodeType;
                        row["FieldKey"] = "";
                        row["FieldCaption"] = "";
                        row["FieldDataType"] = "";
                        row["FieldDataSource"] = "";
                        row["languageTexts"] = "";
                        monthOut.Rows.Add(row);
                    }
                }
            }

            //加载期初分组数据
            LastData.Rows.Clear();
            if (Map.MapProperties.Advanced.Field.Count > 0)
            {
                List<BillDataMapAdvancedElement> StockList = new List<BillDataMapAdvancedElement>();
                foreach (BillDataMapAdvancedElement it in Map.MapProperties.Advanced.Field)
                {
                    if (it.StockType == BeanUtil.STOCK_GROUP_ST_LAST_STOCK)
                    {
                        StockList.Add(it);
                    }
                }

                if (Map.MapProperties.Target != null && Map.MapProperties.Target.Element != null
                    && Map.MapProperties.Target.Element.Count > 0)
                {
                    foreach (BillDataMapTargetElement it in Map.MapProperties.Target.Element)
                    {
                        if (it.NodeType != BeanUtil.STOCK_MAP_FIELD_PRICE)//单价不出现在类型中
                        {
                            DataRow row = LastData.NewRow();
                            row["caption"] = it.Caption;
                            row["key"] = it.Key;
                            row["typeNode"] = it.NodeType;
                            row["FieldKey"] = "";
                            row["FieldCaption"] = "";
                            row["FieldDataType"] = "";
                            row["FieldDataSource"] = "";
                            row["languageTexts"] = "";

                            //查找已存在的字段列表
                            if (StockList.Count > 0)
                            {
                                foreach (BillDataMapAdvancedElement ele in StockList)
                                {
                                    if (it.Key == ele.StockLinkKey)
                                    {
                                        row["FieldKey"] = ele.Key;
                                        row["FieldCaption"] = ele.Caption;
                                        row["FieldDataType"] = ele.DataType;
                                        row["FieldDataSource"] = ele.DataSource;
                                        row["languageTexts"] = ele.LanguageTexts;
                                        StockList.Remove(ele);
                                        break;
                                    }
                                }
                            }
                            LastData.Rows.Add(row);
                        }
                    }
                }
                if (StockList.Count > 0)//无效的列表
                {
                    foreach (BillDataMapAdvancedElement ele in StockList)
                    {
                        DataRow row = LastData.NewRow();
                        row["caption"] = "无效的数据";
                        row["key"] = ele.StockLinkKey;
                        row["typeNode"] = "";
                        row["FieldKey"] = ele.Key;
                        row["FieldCaption"] = ele.Caption;
                        row["FieldDataType"] = ele.DataType;
                        row["FieldDataSource"] = ele.DataSource;
                        row["languageTexts"] = ele.LanguageTexts;
                        LastData.Rows.Add(row);
                    }
                }

            }
            else
            {
                if (Map.MapProperties.Target != null && Map.MapProperties.Target.Element != null
                    && Map.MapProperties.Target.Element.Count > 0)
                {
                    foreach (BillDataMapTargetElement it in Map.MapProperties.Target.Element)
                    {
                        if (it.NodeType != BeanUtil.STOCK_MAP_FIELD_PRICE)//单价不出现在类型中
                        {
                            DataRow row = LastData.NewRow();
                            row["caption"] = it.Caption;
                            row["key"] = it.Key;
                            row["typeNode"] = it.NodeType;
                            row["FieldKey"] = "";
                            row["FieldCaption"] = "";
                            row["FieldDataType"] = "";
                            row["FieldDataSource"] = "";
                            row["languageTexts"] = "";
                            LastData.Rows.Add(row);
                        }
                    }
                }
            }


            //加载当日入分组数据
            dayIn.Rows.Clear();
            if (Map.MapProperties.Advanced.Field.Count > 0)
            {
                List<BillDataMapAdvancedElement> StockList = new List<BillDataMapAdvancedElement>();
                foreach (BillDataMapAdvancedElement it in Map.MapProperties.Advanced.Field)
                {
                    if (it.StockType == BeanUtil.STOCK_GROUP_ST_DAYIN_STOCK)
                    {
                        StockList.Add(it);
                    }
                }

                if (Map.MapProperties.Target != null && Map.MapProperties.Target.Element != null
                    && Map.MapProperties.Target.Element.Count > 0)
                {
                    foreach (BillDataMapTargetElement it in Map.MapProperties.Target.Element)
                    {
                        if (it.NodeType != BeanUtil.STOCK_MAP_FIELD_PRICE)//单价不出现在类型中
                        {
                            DataRow row = dayIn.NewRow();
                            row["caption"] = it.Caption;
                            row["key"] = it.Key;
                            row["typeNode"] = it.NodeType;
                            row["FieldKey"] = "";
                            row["FieldCaption"] = "";
                            row["FieldDataType"] = "";
                            row["FieldDataSource"] = "";
                            row["languageTexts"] = "";

                            //查找已存在的字段列表
                            if (StockList.Count > 0)
                            {
                                foreach (BillDataMapAdvancedElement ele in StockList)
                                {
                                    if (it.Key == ele.StockLinkKey)
                                    {
                                        row["FieldKey"] = ele.Key;
                                        row["FieldCaption"] = ele.Caption;
                                        row["FieldDataType"] = ele.DataType;
                                        row["FieldDataSource"] = ele.DataSource;
                                        row["languageTexts"] = ele.LanguageTexts;
                                        StockList.Remove(ele);
                                        break;
                                    }
                                }
                            }
                            dayIn.Rows.Add(row);
                        }
                    }
                }
                if (StockList.Count > 0)//无效的列表
                {
                    foreach (BillDataMapAdvancedElement ele in StockList)
                    {
                        DataRow row = dayIn.NewRow();
                        row["caption"] = "无效的数据";
                        row["key"] = ele.StockLinkKey;
                        row["typeNode"] = "";
                        row["FieldKey"] = ele.Key;
                        row["FieldCaption"] = ele.Caption;
                        row["FieldDataType"] = ele.DataType;
                        row["FieldDataSource"] = ele.DataSource;
                        row["languageTexts"] = ele.LanguageTexts;
                        dayIn.Rows.Add(row);
                    }
                }

            }
            else
            {
                if (Map.MapProperties.Target != null && Map.MapProperties.Target.Element != null
                    && Map.MapProperties.Target.Element.Count > 0)
                {
                    foreach (BillDataMapTargetElement it in Map.MapProperties.Target.Element)
                    {
                        if (it.NodeType != BeanUtil.STOCK_MAP_FIELD_PRICE)//单价不出现在类型中
                        {
                            DataRow row = dayIn.NewRow();
                            row["caption"] = it.Caption;
                            row["key"] = it.Key;
                            row["typeNode"] = it.NodeType;
                            row["FieldKey"] = "";
                            row["FieldCaption"] = "";
                            row["FieldDataType"] = "";
                            row["FieldDataSource"] = "";
                            row["languageTexts"] = "";
                            dayIn.Rows.Add(row);
                        }
                    }
                }
            }


            //加载当日出分组数据
            dayOut.Rows.Clear();
            if (Map.MapProperties.Advanced.Field.Count > 0)
            {
                List<BillDataMapAdvancedElement> StockList = new List<BillDataMapAdvancedElement>();
                foreach (BillDataMapAdvancedElement it in Map.MapProperties.Advanced.Field)
                {
                    if (it.StockType == BeanUtil.STOCK_GROUP_ST_DAYOUT_STOCK)
                    {
                        StockList.Add(it);
                    }
                }

                if (Map.MapProperties.Target != null && Map.MapProperties.Target.Element != null
                    && Map.MapProperties.Target.Element.Count > 0)
                {
                    foreach (BillDataMapTargetElement it in Map.MapProperties.Target.Element)
                    {
                        if (it.NodeType != BeanUtil.STOCK_MAP_FIELD_PRICE)//单价不出现在类型中
                        {
                            DataRow row = dayOut.NewRow();
                            row["caption"] = it.Caption;
                            row["key"] = it.Key;
                            row["typeNode"] = it.NodeType;
                            row["FieldKey"] = "";
                            row["FieldCaption"] = "";
                            row["FieldDataType"] = "";
                            row["FieldDataSource"] = "";
                            row["languageTexts"] = "";

                            //查找已存在的字段列表
                            if (StockList.Count > 0)
                            {
                                foreach (BillDataMapAdvancedElement ele in StockList)
                                {
                                    if (it.Key == ele.StockLinkKey)
                                    {
                                        row["FieldKey"] = ele.Key;
                                        row["FieldCaption"] = ele.Caption;
                                        row["FieldDataType"] = ele.DataType;
                                        row["FieldDataSource"] = ele.DataSource;
                                        row["languageTexts"] = ele.LanguageTexts;
                                        StockList.Remove(ele);
                                        break;
                                    }
                                }
                            }
                            dayOut.Rows.Add(row);
                        }
                    }
                }
                if (StockList.Count > 0)//无效的列表
                {
                    foreach (BillDataMapAdvancedElement ele in StockList)
                    {
                        DataRow row = dayOut.NewRow();
                        row["caption"] = "无效的数据";
                        row["key"] = ele.StockLinkKey;
                        row["typeNode"] = "";
                        row["FieldKey"] = ele.Key;
                        row["FieldCaption"] = ele.Caption;
                        row["FieldDataType"] = ele.DataType;
                        row["FieldDataSource"] = ele.DataSource;
                        row["languageTexts"] = ele.LanguageTexts;
                        dayOut.Rows.Add(row);
                    }
                }

            }
            else
            {
                if (Map.MapProperties.Target != null && Map.MapProperties.Target.Element != null
                    && Map.MapProperties.Target.Element.Count > 0)
                {
                    foreach (BillDataMapTargetElement it in Map.MapProperties.Target.Element)
                    {
                        if (it.NodeType != BeanUtil.STOCK_MAP_FIELD_PRICE)//单价不出现在类型中
                        {
                            DataRow row = dayOut.NewRow();
                            row["caption"] = it.Caption;
                            row["key"] = it.Key;
                            row["typeNode"] = it.NodeType;
                            row["FieldKey"] = "";
                            row["FieldCaption"] = "";
                            row["FieldDataType"] = "";
                            row["FieldDataSource"] = "";
                            row["languageTexts"] = "";
                            dayOut.Rows.Add(row);
                        }
                    }
                }
            }

            //日志状态
            if (Map.MapProperties.Advanced.LogEnable)
            {
                TabPageDayInto.PageVisible = true;
                TabPageDayOut.PageVisible = true;
            }
            else
            {
                TabPageDayInto.PageVisible = false;
                TabPageDayOut.PageVisible = false;
            }


            if (bill == null || (Map.MapProperties.Advanced.ModKey!="" && bill.getKey() != Map.MapProperties.Advanced.ModKey))
            {
                LoadTimer.Enabled = true;
                if (Map.MapProperties.Advanced.ModKey != "")
                {
                    foreach (ImageComboBoxItem it in ModComboBoxEdit.Properties.Items)
                    {
                        if (it.Value is ProjectMenuCommonElement)
                        {
                            if ((it.Value as ProjectMenuCommonElement).Command == Map.MapProperties.Advanced.ModKey)
                            {
                                ModComboBoxEdit.SelectedItem = it;
                            }
                        }
                    }
                }
                else
                {
                    ModComboBoxEdit.SelectedIndex = 0;
                }
            }
            else
            {
                FinisLoad();
            }
            return base.ShowDialog();
        } 

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadBill()
        {
            if(ModComboBoxEdit.SelectedItem!=null&& (ModComboBoxEdit.SelectedItem as ImageComboBoxItem).Value != null)
            {
                ImageComboBoxItem select = ModComboBoxEdit.SelectedItem as ImageComboBoxItem;
                if(select.Value is ProjectMenuCommonElement)
                {
                    ProjectMenuCommonElement ele = select.Value as ProjectMenuCommonElement;
                    ProgressBarControl.Visible = true;
                    bool read = false;//不验证数据
                    RootPanel.Http.AsyncResourcesXmlBean(ele.Command,ele.Caption, ProjectUtil.TYPE_BILL_MODULE, ref read, null, ThreadLoadXml);
                }
            }
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ThreadLoadXml(object sender,PropertyDataEvent e)
        {
            if(Thread.CurrentThread.ManagedThreadId != mainThreadId)
            {
                BeginInvoke(new KEventHandler(ThreadLoadXml), new object[] { sender, e });
            }
            else
            {
                bool success = false;
                if (e.Key == "OK")//加载成功
                {
                    string xml = e.Value as string;
                    JAXBContext<BillModuleItem> jaxb = new JAXBContext<BillModuleItem>();
                    try
                    {
                        bill = jaxb.unmarshal(xml);
                        success = true;
                    }
                    catch (Exception ee)
                    {
                        RootPanel.AddConsoleText(ee.Message, true);
                    }
                    if (bill == null)
                    {
                        RootPanel.ShowAlert("提示", "xml解析失败");
                    }
                }
                else
                {
                    RootPanel.ShowAlert("提示","加载配置异常:" + e.Value.ToString());
                }

                if (success)
                {
                    FinisLoad();
                }
                else
                {
                    Text = "库存对象设置";
                }
                
                ProgressBarControl.Visible = false;
            }
        }


        private void FinisLoad()
        {
            Text = string.Format("库存对象设置-{0}", bill.getCaption());
            //加载分组字段选择项
            RepositoryGroupFieldEdit.Items.Clear();
            fieldList.Clear();
            //加载结存字段选择项目
            RepositoryRealFieldList.Items.Clear();
            //加载结存月入选择项目
            RepositoryMonInFieldList.Items.Clear();
            //加载结存月出选择项目
            RepositoryMonthOutList.Items.Clear();
            //加载结存期初选择项目
            RepositoryLastList.Items.Clear();
            //加载结存日入选择项目
            RepositoryDayInList.Items.Clear();
            RepositoryDayOutList.Items.Clear();
            if (bill.UiField != null && bill.UiField.FieldsProperties != null
                && bill.UiField.FieldsProperties.Count > 0)
            {
                foreach (BillUiGroupItems gIt in bill.UiField.FieldsProperties)
                {
                    if (!gIt.Table)
                    {
                        if (gIt.Fields != null && gIt.Fields.Count > 0)
                        {
                            foreach (BillUiFieldsItem field in gIt.Fields)
                            {
                                FieldSelectItem item = new FieldSelectItem(field.Key, string.Format("{0}.{1}", bill.getCaption(), field.Caption), field.DataProperty.DataType, field.DataProperty.DataSource, field.LanguageTexts);
                                fieldList.Add(item);
                                if (item.DataType == BeanUtil.KT_DTSTRING || item.DataType == BeanUtil.KT_DTDATETIME)
                                {
                                    RepositoryGroupFieldEdit.Items.Add(item);
                                }
                                else if (item.DataType == BeanUtil.KT_DTFLOAT || item.DataType == BeanUtil.KT_DTINTEGER)
                                {
                                    RepositoryRealFieldList.Items.Add(item.Caption);
                                    RepositoryMonInFieldList.Items.Add(item.Caption);
                                    RepositoryMonthOutList.Items.Add(item.Caption);
                                    RepositoryLastList.Items.Add(item.Caption);
                                    RepositoryDayInList.Items.Add(item.Caption);
                                    RepositoryDayOutList.Items.Add(item.Caption);
                                }
                            }
                        }
                    }
                }
            }
            //刷新分组列表内容
            if (fieldList.Count > 0 && group.Rows.Count > 0)
            {
                foreach (DataRow row in group.Rows)
                {
                    foreach (FieldSelectItem field in fieldList)
                    {
                        if (row["FieldKey"].Equals(field.Key) && row["FieldCaption"].Equals(field.Caption))
                        {
                            row["FieldObject"] = field;
                            break;
                        }
                    }
                }

            }
            GridGroupView.RefreshData();
        }

        //清空现在配置
        private void ClearTable()
        {
            group.Rows.Clear();
        }

        #region 事件注册区域

        //保存数据
        private void BtnSubmit_Click(object sender, System.EventArgs e)
        {
            if (bill == null) return;
            Map.MapProperties.Advanced.ModCaption = bill.getCaption();
            Map.MapProperties.Advanced.ModKey = bill.getKey();
            Map.MapProperties.Advanced.ModLanguageTexts = bill.getLanguageText();
            Map.MapProperties.Advanced.ModTableName = "";
            Map.MapProperties.Advanced.LogEnable = HasDayInAndOutType.Checked;

            Map.MapProperties.Advanced.Field.Clear();
            //保存分组数据
            if (group.Rows.Count > 0 )
            {
                foreach(DataRow row in group.Rows)
                {
                    BillDataMapAdvancedElement item = new BillDataMapAdvancedElement();
                    item.Caption = (string)row["FieldCaption"];
                    item.Key = (string)row["FieldKey"];
                    item.DataType = (string)row["FieldDataType"];
                    item.DataSource = (string)row["FieldDataSource"];
                    item.LanguageTexts = (string)row["languageTexts"];
                    item.Command = (string)row["GroupType"];
                    item.StockLinkKey = "";
                    item.StockType = "";
                    Map.MapProperties.Advanced.Field.Add(item);
                }
            }
            //保存结存数据
            if (stock.Rows.Count > 0)
            {
                foreach(DataRow row in stock.Rows)
                {
                    BillDataMapAdvancedElement item = new BillDataMapAdvancedElement();
                    item.Caption = (string)row["FieldCaption"];
                    item.Key = (string)row["FieldKey"];
                    item.DataType = (string)row["FieldDataType"];
                    item.DataSource = (string)row["FieldDataSource"];
                    item.LanguageTexts = (string)row["languageTexts"];
                    item.Command = (string)row["typeNode"];
                    item.StockLinkKey = (string)row["key"];
                    item.StockType = BeanUtil.STOCK_GROUP_ST_STOCK;
                    if(!row["caption"].Equals("无效的数据"))
                        Map.MapProperties.Advanced.Field.Add(item);
                }
            }
            //保存月入数据
            if (monthIn.Rows.Count > 0)
            {
                foreach (DataRow row in monthIn.Rows)
                {
                    BillDataMapAdvancedElement item = new BillDataMapAdvancedElement();
                    item.Caption = (string)row["FieldCaption"];
                    item.Key = (string)row["FieldKey"];
                    item.DataType = (string)row["FieldDataType"];
                    item.DataSource = (string)row["FieldDataSource"];
                    item.LanguageTexts = (string)row["languageTexts"];
                    item.Command = (string)row["typeNode"];
                    item.StockLinkKey = (string)row["key"];
                    item.StockType = BeanUtil.STOCK_GROUP_ST_INTO_STOCK;
                    if (!row["caption"].Equals("无效的数据"))
                        Map.MapProperties.Advanced.Field.Add(item);
                }
            }

            //保存月出数据
            if (monthOut.Rows.Count > 0)
            {
                foreach (DataRow row in monthOut.Rows)
                {
                    BillDataMapAdvancedElement item = new BillDataMapAdvancedElement();
                    item.Caption = (string)row["FieldCaption"];
                    item.Key = (string)row["FieldKey"];
                    item.DataType = (string)row["FieldDataType"];
                    item.DataSource = (string)row["FieldDataSource"];
                    item.LanguageTexts = (string)row["languageTexts"];
                    item.Command = (string)row["typeNode"];
                    item.StockLinkKey = (string)row["key"];
                    item.StockType = BeanUtil.STOCK_GROUP_ST_OUT_STOCK;
                    if (!row["caption"].Equals("无效的数据"))
                        Map.MapProperties.Advanced.Field.Add(item);
                }
            }
            //保存期初分组数据
            if (LastData.Rows.Count > 0)
            {
                foreach (DataRow row in LastData.Rows)
                {
                    BillDataMapAdvancedElement item = new BillDataMapAdvancedElement();
                    item.Caption = (string)row["FieldCaption"];
                    item.Key = (string)row["FieldKey"];
                    item.DataType = (string)row["FieldDataType"];
                    item.DataSource = (string)row["FieldDataSource"];
                    item.LanguageTexts = (string)row["languageTexts"];
                    item.Command = (string)row["typeNode"];
                    item.StockLinkKey = (string)row["key"];
                    item.StockType = BeanUtil.STOCK_GROUP_ST_LAST_STOCK;
                    if (!row["caption"].Equals("无效的数据"))
                        Map.MapProperties.Advanced.Field.Add(item);
                }
            }
            if (HasDayInAndOutType.Checked)
            {
                //保存日入分组数据
                if (dayIn.Rows.Count > 0)
                {
                    foreach (DataRow row in dayIn.Rows)
                    {
                        BillDataMapAdvancedElement item = new BillDataMapAdvancedElement();
                        item.Caption = (string)row["FieldCaption"];
                        item.Key = (string)row["FieldKey"];
                        item.DataType = (string)row["FieldDataType"];
                        item.DataSource = (string)row["FieldDataSource"];
                        item.LanguageTexts = (string)row["languageTexts"];
                        item.Command = (string)row["typeNode"];
                        item.StockLinkKey = (string)row["key"];
                        item.StockType = BeanUtil.STOCK_GROUP_ST_DAYIN_STOCK;
                        if (!row["caption"].Equals("无效的数据"))
                            Map.MapProperties.Advanced.Field.Add(item);
                    }
                }

                //保存日入分组数据
                if (dayOut.Rows.Count > 0)
                {
                    foreach (DataRow row in dayOut.Rows)
                    {
                        BillDataMapAdvancedElement item = new BillDataMapAdvancedElement();
                        item.Caption = (string)row["FieldCaption"];
                        item.Key = (string)row["FieldKey"];
                        item.DataType = (string)row["FieldDataType"];
                        item.DataSource = (string)row["FieldDataSource"];
                        item.LanguageTexts = (string)row["languageTexts"];
                        item.Command = (string)row["typeNode"];
                        item.StockLinkKey = (string)row["key"];
                        item.StockType = BeanUtil.STOCK_GROUP_ST_DAYOUT_STOCK;
                        if (!row["caption"].Equals("无效的数据"))
                            Map.MapProperties.Advanced.Field.Add(item);
                    }
                }
            }
            


            UnitPanel.SaveStatus = false;
        }

               

        /// <summary>
        /// 分组字段选项值改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GridGroupView_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if(e.Column.FieldName == "FieldObject")
            {
                FieldSelectItem it = group.Rows[e.RowHandle]["FieldObject"] as FieldSelectItem;
                if (it != null)
                {
                    DataRow row = group.Rows[e.RowHandle];
                    row["FieldObject"] = it;
                    row["FieldCaption"] = it.Caption;
                    row["FieldKey"] = it.Key;
                    row["FieldDataType"] = it.DataType;
                    row["FieldDataSource"] = it.DataSource;
                    row["languageTexts"] = it.LanguageTexts;
                    GridGroupView.RefreshRow(e.RowHandle);
                }
            }
        }

        private void GridRealView_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if(e.Column.FieldName == "FieldCaption")
            {
                DataRow row = GridRealView.GetDataRow(e.RowHandle);
                string caption = (string)row["FieldCaption"];
                if (fieldList.Count > 0)
                {
                    foreach(FieldSelectItem field in fieldList)
                    {
                        if(field.Caption == caption)
                        {
                            row["FieldKey"] = field.Key;
                            row["FieldDataType"] = field.DataType;
                            row["FieldDataSource"] = field.DataSource;
                            row["languageTexts"] = field.LanguageTexts;
                            break;
                        }
                    }
                }
            }
        }

        private void GridMonInView_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.FieldName == "FieldCaption")
            {
                DataRow row = GridMonInView.GetDataRow(e.RowHandle);
                string caption = (string)row["FieldCaption"];
                if (fieldList.Count > 0)
                {
                    foreach (FieldSelectItem field in fieldList)
                    {
                        if (field.Caption == caption)
                        {
                            row["FieldKey"] = field.Key;
                            row["FieldDataType"] = field.DataType;
                            row["FieldDataSource"] = field.DataSource;
                            row["languageTexts"] = field.LanguageTexts;
                            break;
                        }
                    }
                }
            }
        }

        private void GridMonthOutView_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.FieldName == "FieldCaption")
            {
                DataRow row = GridMonthOutView.GetDataRow(e.RowHandle);
                string caption = (string)row["FieldCaption"];
                if (fieldList.Count > 0)
                {
                    foreach (FieldSelectItem field in fieldList)
                    {
                        if (field.Caption == caption)
                        {
                            row["FieldKey"] = field.Key;
                            row["FieldDataType"] = field.DataType;
                            row["FieldDataSource"] = field.DataSource;
                            row["languageTexts"] = field.LanguageTexts;
                            break;
                        }
                    }
                }
            }
        }

        private void GridDayInView_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.FieldName == "FieldCaption")
            {
                DataRow row = GridDayInView.GetDataRow(e.RowHandle);
                string caption = (string)row["FieldCaption"];
                if (fieldList.Count > 0)
                {
                    foreach (FieldSelectItem field in fieldList)
                    {
                        if (field.Caption == caption)
                        {
                            row["FieldKey"] = field.Key;
                            row["FieldDataType"] = field.DataType;
                            row["FieldDataSource"] = field.DataSource;
                            row["languageTexts"] = field.LanguageTexts;
                            break;
                        }
                    }
                }
            }
        }

        private void GridDayOutView_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.FieldName == "FieldCaption")
            {
                DataRow row = GridDayOutView.GetDataRow(e.RowHandle);
                string caption = (string)row["FieldCaption"];
                if (fieldList.Count > 0)
                {
                    foreach (FieldSelectItem field in fieldList)
                    {
                        if (field.Caption == caption)
                        {
                            row["FieldKey"] = field.Key;
                            row["FieldDataType"] = field.DataType;
                            row["FieldDataSource"] = field.DataSource;
                            row["languageTexts"] = field.LanguageTexts;
                            break;
                        }
                    }
                }
            }
        }

        private void HasDayInAndOutType_CheckedChanged(object sender, EventArgs e)
        {
            if (HasDayInAndOutType.Checked)
            {
                TabPageDayInto.PageVisible = true;
                TabPageDayOut.PageVisible = true;
            }
            else
            {
                TabPageDayInto.PageVisible = false;
                TabPageDayOut.PageVisible = false;
            }
        }

        private void GridLastView_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.FieldName == "FieldCaption")
            {
                DataRow row = GridLastView.GetDataRow(e.RowHandle);
                string caption = (string)row["FieldCaption"];
                if (fieldList.Count > 0)
                {
                    foreach (FieldSelectItem field in fieldList)
                    {
                        if (field.Caption == caption)
                        {
                            row["FieldKey"] = field.Key;
                            row["FieldDataType"] = field.DataType;
                            row["FieldDataSource"] = field.DataSource;
                            row["languageTexts"] = field.LanguageTexts;
                            break;
                        }
                    }
                }
            }
        }

        private void ModComboBoxEdit_SelectedValueChanged(object sender, System.EventArgs e)
        {
            if (LoadTimer.Enabled) return;
            if (bill != null && (ModComboBoxEdit.SelectedItem as ImageComboBoxItem).Description != bill.getCaption())
            {
                if (XtraMessageBox.Show("您确认修改目标库存对象吗？如果修改现有配置会被清空", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
                {
                    ClearTable();
                    bill = null;
                    LoadBill();
                }
                else
                {
                    foreach(ImageComboBoxItem it in ModComboBoxEdit.Properties.Items)
                    {
                        ProjectMenuCommonElement ele = it.Value as ProjectMenuCommonElement;
                        if(ele!=null && ele.Command == bill.getKey())
                        {
                            ModComboBoxEdit.SelectedItem = it;
                            break;
                        }
                    }
                }
            }
            else
            {
                if(bill!= null && (ModComboBoxEdit.SelectedItem as ImageComboBoxItem).Description == bill.getCaption())
                {

                }
                else
                {
                    bill = null;
                    LoadBill();
                }
            }
            
        }

        
        /// <summary>
        /// 添加行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnAdd_Click(object sender, System.EventArgs e)
        {
            if (bill == null)
            {
                RootPanel.ShowAlert("提示", "请选择模块对象再进行此操作");
                return;
            }
            
            if(TabControlPage.SelectedTabPageIndex == 0)
            {
                DataRow row = group.NewRow();
                row["FieldObject"] = null;
                row["FieldCaption"] = "";
                row["FieldKey"] = "";
                row["FieldDataType"] = "";
                row["FieldDataSource"] = "";
                row["languageTexts"] = "";
                row["GroupType"] = BeanUtil.MAP_STGROUP;
                group.Rows.Add(row);
                GridGroupView.RefreshData();
                GridGroupView.MoveLast();
            }
        }
        /// <summary>
        /// 删除行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddRemove_Click(object sender, EventArgs e)
        {
            if (TabControlPage.SelectedTabPageIndex == 0)
            {
                if (GridGroupView.FocusedRowHandle >= 0 && group.Rows.Count>0)
                {
                    int pevIndex = -1;
                    if (GridGroupView.FocusedRowHandle > 0)
                    {
                        pevIndex = GridGroupView.FocusedRowHandle - 1;
                    }
                    else
                    {
                        if (group.Rows.Count > 1)
                        {
                            pevIndex = 0;
                        }
                    }
                    
                    group.Rows.RemoveAt(GridGroupView.FocusedRowHandle);
                    GridGroupView.RefreshData();
                    if (pevIndex >= 0)
                    {
                        GridGroupView.FocusedRowHandle = pevIndex;
                    }
                }
            }
        }

        private void LoadTimer_Tick(object sender, EventArgs e)
        {
            LoadBill();
            LoadTimer.Enabled = false;
        }


        #endregion

    }



    /// <summary>
    /// 字段简明内容下拉选项
    /// </summary>
    public class FieldSelectItem
    {
        private string key = "";
        private string caption = "";
        private string dataType = "";
        private string dataSource = "";
        private string languageTexts = "";


        public FieldSelectItem(string _key,string _caption,string _dataType,string _dataSource,string language)
        {
            key = _key;
            caption = _caption;
            dataType = _dataType;
            dataSource = _dataSource;
            languageTexts = language;
        }

        public string Key
        {
            get { return key; }
            set { key = value; }
        }

        public string Caption
        {
            get { return caption; }
            set { caption = value; }
        }

        public string DataType
        {
            get { return dataType; }
            set { dataType = value; }
        }

        public string DataSource
        {
            get { return dataSource; }
            set { dataSource = value; }
        }

        public string LanguageTexts
        {
            get { return languageTexts; }
            set { languageTexts = value; }
        }

        public override string ToString()
        {
            return caption;
        }
    }
}